package com.bolt.report.business.modules.dataset.dto.converter;

import com.bolt.common.utils.StrUtil;
import com.bolt.report.business.modules.dataset.dto.ReportDataSetDTO;
import com.bolt.report.business.modules.dataset.entity.ReportDataSetEntity;
import com.bolt.report.business.modules.datasource.dto.converter.ReportDataSourceConverter;
import com.bolt.report.business.modules.datasource.entity.ReportDataSourceEntity;
import com.bolt.report.business.modules.field.dto.converter.ReportDbFieldConverter;
import com.bolt.report.business.modules.param.dto.converter.ReportDbParamConverter;
import com.bolt.report.definition.DataSetDefinition;
import com.bolt.report.definition.DataSetType;

import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/27.
 */
public class ReportDataSetConverter {
    private ReportDataSetConverter() {
// do nothing
    }

    /**
     * Entity对象转换为DTO对象
     *
     * @param reportDb DO对象
     * @return DTO对象
     */
    public static ReportDataSetDTO toDTO(ReportDataSetEntity reportDb) {
        if (Objects.isNull(reportDb)) {
            return null;
        }
        ReportDataSetDTO result = new ReportDataSetDTO();
        result.setId(reportDb.getId());
        result.setDbCode(reportDb.getDbCode());
        result.setDbChName(reportDb.getDbChName());
        result.setDbType(reportDb.getDbType());
        result.setDbTableName(reportDb.getDbTableName());
        result.setDbDynSql(reportDb.getDbDynSql());
        result.setDbKey(reportDb.getDbKey());
        result.setTbDbKey(reportDb.getTbDbKey());
        result.setTbDbTableName(reportDb.getTbDbTableName());
        result.setSpringKey(reportDb.getSpringKey());
        result.setList(reportDb.isList());
        result.setPage(reportDb.isPage());
        result.setDbSourceId(reportDb.getDbSourceId());
        result.setJsonData(reportDb.getJsonData());
        result.setApiConvert(reportDb.getApiConvert());
        result.setApiMethod(reportDb.getApiMethod());
        result.setApiRequestType(reportDb.getApiRequestType());
        result.setApiUrl(reportDb.getApiUrl());
        result.setReportDataSource(ReportDataSourceConverter.toDTO(reportDb.getDataSourceEntity()));
        result.setFields(ReportDbFieldConverter.toDTO(reportDb.getFields()));
        result.setParams(ReportDbParamConverter.toDTO(reportDb.getParams()));

        // TODO
        return result;
    }

    /**
     * DTO对象转换为Entity对象
     *
     * @param reportDb DO对象
     * @return VO对象
     */
    public static ReportDataSetEntity toEntity(ReportDataSetDTO reportDb) {
        if (Objects.isNull(reportDb)) {
            return null;
        }
        ReportDataSetEntity result = new ReportDataSetEntity();
        if(StrUtil.isNotBlank(reportDb.getId())){
            result.setId(reportDb.getId());
        }
        result.setDbCode(reportDb.getDbCode());
        result.setDbChName(reportDb.getDbChName());
        result.setDbType(reportDb.getDbType());
        result.setDbTableName(reportDb.getDbTableName());
        result.setDbDynSql(reportDb.getDbDynSql());
        result.setDbKey(reportDb.getDbKey());
        result.setTbDbKey(reportDb.getTbDbKey());
        result.setTbDbTableName(reportDb.getTbDbTableName());
        result.setSpringKey(reportDb.getSpringKey());

        result.setList(reportDb.isList());
        result.setPage(reportDb.isPage());
        result.setDbSourceId(reportDb.getDbSourceId());
        result.setJsonData(reportDb.getJsonData());
        result.setApiConvert(reportDb.getApiConvert());
        result.setApiMethod(reportDb.getApiMethod());
        result.setApiRequestType(reportDb.getApiRequestType());
        result.setApiUrl(reportDb.getApiUrl());
        if (StrUtil.isNotBlank(reportDb.getDbSourceId())) {
            ReportDataSourceEntity dataSourceEntity = new ReportDataSourceEntity();
            dataSourceEntity.setId(reportDb.getDbSourceId());
            result.setDataSourceEntity(dataSourceEntity);
        }
        // TODO
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param reportDb DO对象
     * @return VO对象
     */
    public static ReportDataSetEntity toEntity(ReportDataSetEntity result, ReportDataSetDTO reportDb) {
        if (Objects.isNull(reportDb)) {
            return null;
        }

        result.setDbCode(reportDb.getDbCode());
        result.setDbChName(reportDb.getDbChName());
        result.setDbType(reportDb.getDbType());
        result.setDbTableName(reportDb.getDbTableName());
        result.setDbDynSql(reportDb.getDbDynSql());
        result.setDbKey(reportDb.getDbKey());
        result.setTbDbKey(reportDb.getTbDbKey());
        result.setTbDbTableName(reportDb.getTbDbTableName());
        result.setSpringKey(reportDb.getSpringKey());
        result.setList(reportDb.isList());
        result.setPage(reportDb.isPage());
        result.setDbSourceId(reportDb.getDbSourceId());
        result.setJsonData(reportDb.getJsonData());
        result.setApiConvert(reportDb.getApiConvert());
        result.setApiMethod(reportDb.getApiMethod());
        result.setApiRequestType(reportDb.getApiRequestType());
        result.setApiUrl(reportDb.getApiUrl());
        if (StrUtil.isNotBlank(reportDb.getDbSourceId())) {
            ReportDataSourceEntity dataSourceEntity = new ReportDataSourceEntity();
            dataSourceEntity.setId(reportDb.getDbSourceId());
            result.setDataSourceEntity(dataSourceEntity);
        }
        return result;
    }

    public static DataSetDefinition toDataSetDefinition(ReportDataSetEntity entity) {
        DataSetDefinition dataSetDefinition = new DataSetDefinition();
        dataSetDefinition.setQuerySql(entity.getDbDynSql());
        dataSetDefinition.setType(DataSetType.toType(entity.getDbType()));
        dataSetDefinition.setApiUrl(entity.getApiUrl());
        dataSetDefinition.setApiMethod(entity.getApiMethod());
        dataSetDefinition.setJsonData(entity.getJsonData());
        dataSetDefinition.setApiConvert(entity.getApiConvert());
        dataSetDefinition.setKey(entity.getDbKey());
        dataSetDefinition.setList(entity.isList());
        dataSetDefinition.setPage(entity.isPage());
        dataSetDefinition.setSpringKey(entity.getSpringKey());
        dataSetDefinition.setApiRequestType(entity.getApiRequestType());
        dataSetDefinition.setName(entity.getDbChName());
        dataSetDefinition.setParameters(ReportDbParamConverter.toDefinition(ReportDbParamConverter.toDTO(entity.getParams())) );
        return dataSetDefinition;

    }

    /**
     * 批量 Entity对象转换为DTO对象
     *
     * @param reportDbList Entity集合对象
     * @return DTO对象
     */
    public static Set<ReportDataSetDTO> toDTO(Collection<ReportDataSetEntity> reportDbList) {
        if (Objects.isNull(reportDbList)) {
            return null;
        }
        return reportDbList.stream().map(ReportDataSetConverter::toDTO).collect(Collectors.toSet());
    }

}
